package server;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.security.PrivateKey;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.BlockingQueue;

import encryption.RSADecryptor;
import frame.Frame;

public class Task implements Runnable {
	
	private byte[] frame;
	private SimpleDateFormat sdf;
	
	private String decryptedFrame;
	
	private BlockingQueue<Frame> persistentQueue;
	
	public Task(byte[] frame, BlockingQueue<Frame> persistentQueue){
		this.frame = frame;
		
		this.persistentQueue = persistentQueue;
		
		sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
	}
	
	public void run(){
		
		try {
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(RSADecryptor.PRIVATE_KEY_FILE));
			PrivateKey privateKey = (PrivateKey) ois.readObject();
			ois.close();
			
			decryptedFrame = RSADecryptor.decrypt(frame, privateKey);
			decryptedFrame.trim();
			
			Frame frame = buildFrame();
			
			if(frame.getCodEvento()==1){
				frame.toPaint();
			}
			else{
				frame.toPaintAlert();
			}
			
			persistentQueue.put(frame);
			
			System.out.println(new String(frame.getOriginalFrame()) + " procesada " + sdf.format(new Date()));
			
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public Frame buildFrame(){
		
		String codVehiculo = decryptedFrame.substring(0,6);
		int codEvento = Integer.parseInt(decryptedFrame.substring(6,8));
		Calendar fecha = Calendar.getInstance();
		fecha.set(Calendar.YEAR, Integer.parseInt(decryptedFrame.substring(8,12)));
		fecha.set(Calendar.MONTH, Integer.parseInt(decryptedFrame.substring(12,14)));
		fecha.set(Calendar.DATE, Integer.parseInt(decryptedFrame.substring(14,16)));
		int segs = Integer.parseInt(decryptedFrame.substring(16,21));
		int horas = (int)((segs / 3600) % 24);
		int minutos = (int)((segs / 60) % 60);
		int segundos = (int)((segs / 1) % 60);
		fecha.set(Calendar.HOUR_OF_DAY, horas);
		fecha.set(Calendar.MINUTE, minutos);
		fecha.set(Calendar.SECOND, segundos);
		fecha.set(Calendar.MILLISECOND, 0);
		double longitudEntero = Double.parseDouble(decryptedFrame.substring(21,24));
		double longitudDecimal = Double.parseDouble(decryptedFrame.substring(21,22)+ "0." +decryptedFrame.substring(24,29));
		double longitud = longitudEntero + longitudDecimal;
		double latitudEntero = Double.parseDouble(decryptedFrame.substring(29,33));
		double latitudDecimal = Double.parseDouble(decryptedFrame.substring(29,30) + "0." + decryptedFrame.substring(33,38));
		double latitud = latitudEntero + latitudDecimal;
		int velocidad = Integer.parseInt(decryptedFrame.substring(38,41));
		int direccion = Integer.parseInt(decryptedFrame.substring(41,44));
		int fuente = Integer.parseInt(decryptedFrame.substring(44,45));
		int edadDato = Integer.parseInt(decryptedFrame.substring(45,46));
		int tempAct = Integer.parseInt(decryptedFrame.substring(46,47));
		int tempVal = Integer.parseInt(decryptedFrame.substring(47,50));
		int choque = Integer.parseInt(decryptedFrame.substring(50,51));
		String puertas = decryptedFrame.substring(51,57);
		int ignicion = Integer.parseInt(decryptedFrame.substring(57,58)); 
		
		Frame f = new Frame(decryptedFrame.getBytes(), codVehiculo, codEvento, new Date(fecha.getTimeInMillis()), longitud, 
				latitud, velocidad, direccion, fuente, edadDato, tempAct, tempVal, choque, puertas, ignicion);
		
		//f.setName(name);
		
		return f;
	}	

}
